package pl.edu.pw.MidEmu.Common.Models;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.Serializable;
import java.util.Vector;

import pl.edu.pw.MidEmu.Common.Consts;

/**
 * Model class representing CBRD component
 * Contains CBDR settings
 * @author Mateusz Starodab
 *
 */
public class CBRDModel extends ClassSettingsModel implements Serializable
{

	private static final long serialVersionUID = -8014528560486422262L;

	/**
	 * Loads CBRD model from file
	 * @return CBRD Model
	 */
	public static CBRDModel load()
	{
		CBRDModel model = null;
		try
		{
			File file = new File(Consts.FilePath_CBRDSettings);
			if (file.exists())
			{
				XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(new FileInputStream(file)));
				model = (CBRDModel) decoder.readObject();
				decoder.close();
			}
		}
		catch (FileNotFoundException ex)
		{
			ex.printStackTrace();
		}
		return model;
	}

	private String CBRDBinDirLocation;

	private String CBRid;

	private String ExecutableFileName;

	@Override
	/**
	 * Compares two CBRD Models
	 * @return True - if CBRD Models are equal
	 */
	public boolean equals(Object obj)
	{
		if (obj != null)
			if (obj instanceof CBRDModel)
				return this.CBRid.equals(((CBRDModel) obj).getCBRid());
		return false;
	}
	
	/**
	 * Gets CBRD root directory location
	 * @return CBRD root directory
	 */
	public String getCBRDBinDirLocation()
	{
		return CBRDBinDirLocation;
	}

	/**
	 * Gets CBRD work directory location
	 * @return CBRD work directory
	 */
	public String getCBRDWorkDirLocation()
	{
		return CBRDBinDirLocation;
	}

	/**
	 * Gets CBRD id
	 * @return CBRD id
	 */
	public String getCBRid()
	{
		return CBRid;
	}

	@Override
	/**
	 * Gets CBRD settings for display in generic data grid
	 */
	public Vector<Vector<String>> getClassData()
	{
		Vector<Vector<String>> data = new Vector<Vector<String>>();

		addData(data, "Binaries directory", CBRDBinDirLocation);
		addData(data, "Executable name", ExecutableFileName);
		addData(data, "CBRD id", CBRid);

		return data;
	}

	/**
	 * Gets CBRD executable file name 
	 * @return CBRD executable file name
	 */
	public String getExecutableFileName()
	{
		return ExecutableFileName;
	}

	@Override
	/**
	 * Gets hash code
	 */
	public int hashCode()
	{
		return super.hashCode();
	}

	/**
	 * Saves CBRD model to xml file for further reading
	 */
	public void save()
	{
		try
		{
			XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(new FileOutputStream(Consts.FilePath_CBRDSettings)));
			encoder.writeObject(this);
			encoder.close();
		}
		catch (FileNotFoundException ex)
		{
			ex.printStackTrace();
		}
	}

	/**
	 * Sets CBRD binaries directory location
	 * @param binDirLocation Binaries directory 
	 */
	public void setCBRDBinDirLocation(String binDirLocation)
	{
		CBRDBinDirLocation = binDirLocation;
	}

	/**
	 * Sets CBRD id
	 * @param rid id
	 */
	public void setCBRid(String rid)
	{
		CBRid = rid;
	}

	/**
	 * Sets executable file name
	 * @param executableFileName Executable file name
	 */
	public void setExecutableFileName(String executableFileName)
	{
		ExecutableFileName = executableFileName;
	}

	@Override
	/**
	 * Returns string representation of this CBRD model
	 */
	public String toString()
	{
		return "CBRD - (Node id: " + CBRid + ")";
	}
}
